package preProcessTrace;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.Scanner;

import static utils.Utils.writeLineToDstFile;

public class ModifyTraceFile {
    /**
     * 删除源trace文件中满足deletedTargetTraceFeature特点的trace，然后写入dstTraceFile中
     *
     * @param srcTraceFile              源trace文件
     * @param dstTraceFile              目标trace文件
     * @param deletedTargetTraceFeature 待删除trace的特点
     */
    public static void deleteTraceInTraceFile(String srcTraceFile, String dstTraceFile, String[] deletedTargetTraceFeature) throws FileNotFoundException {
        Scanner scanner = new Scanner(new FileReader(srcTraceFile));
        tag:
        while (scanner.hasNextLine()) {
            String line = scanner.nextLine();
            for (String deletedFeature : deletedTargetTraceFeature) {
                // 判断line有没有满足删除条件
                if (line.contains(deletedFeature)) {
                    // 满足删除条件
                    continue tag;
                }
            }
            // line不满足删除条件，将line写入目标文件
            writeLineToDstFile(line, dstTraceFile);
        }
        scanner.close();
    }

    public static void main(String[] args) throws FileNotFoundException {
        // 精简一次大概耗时 6.6min
        long startTime = System.currentTimeMillis();

        String srcTraceFile = "src/main/java/methodCallTreeGen/a_raw10.txt";
        String dstTraceFile = "src/main/java/methodCallTreeGen/a.txt";
        String[] deletedTargetTraceFeature = new String[]{"M:sun/nio/fs/UnixNativeDispatcher.stat0 (JLsun/nio/fs/UnixFileAttributes;)V",
                "M:jdk/internal/reflect/NativeConstructorAccessorImpl.newInstance0 (Ljava/lang/reflect/Constructor;[Ljava/lang/Object;)Ljava/lang/Object;",
        "M:java/lang/Object.clone ()Ljava/lang/Object;"};
        deleteTraceInTraceFile(srcTraceFile, dstTraceFile, deletedTargetTraceFeature);

        long endTime = System.currentTimeMillis();
        long executionTime = endTime - startTime;
        System.out.println("用时: " + executionTime / 1000.0 / 3600.0 + "小时");
    }
}
